home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / 1993-OCT.ZIP / EIFFEL.ASC < prev    next >
Text File  |  1993-09-17  |  5KB  |  224 lines

  1. _THE EIFFEL PROGRAMMING LANGUAGE_
  2. by Robert Howard
  3.  
  4.  
  5. Example 1:
  6.  
  7.    is_empty : BOOLEAN is
  8.          -- return TRUE if LIST is empty
  9.       do
  10.          Result := ( head = void )
  11.       end ; -- is_empty
  12.    remove is
  13.          -- remove the first list item
  14.       require
  15.          not_empty: not is_empty
  16.       do
  17.          head := head.next ;
  18.          if head = void
  19.           then
  20.             tail := void ;
  21.          end;
  22.       end ; -- remove
  23.  
  24.  
  25. Example 2:
  26.  
  27.    item : T is
  28.          -- return the head of the list
  29.       require
  30.          not is_empty
  31.       do
  32.          Result := head
  33.       end ; -- item
  34.  
  35.  
  36.  
  37. Example 3:
  38.  
  39.       local
  40.          this_list : MY_LIST[PRINTABLE] ;
  41.          pr : PRINTABLE ;
  42.       do
  43.          -- create the list
  44.          !!this_list ; 
  45.          -- add items to the list (not shown)
  46.          ...
  47.          -- fetch an item from the list
  48.          if not this_list.is_empty
  49.           then 
  50.             pr := this_list.item ;
  51.             pr.print_self ; -- dynamic binding still works!
  52.          end
  53.  
  54.  
  55. Example 4:
  56.  
  57.          pt ?= this_list.item ;
  58.          if pt /= void
  59.           then -- it was a MY_POINT
  60.             pt.print_self ;
  61.          end ;
  62.  
  63.  
  64.  
  65. EIFFEL SOURCE CODE EXAMPLE
  66.  
  67. class DRIVER
  68.    -- In Eiffel, the top level driver is an object, too.
  69. inherit
  70.    BASIC_IO
  71. creation
  72.    make
  73. feature {ANY}
  74.    make is
  75.      -- run this test driver
  76.       local
  77.      list1, list2 : MY_LIST[PRINTABLE] ;
  78.      n1, n2 : MY_NUMBER ; -- a kind of PRINTABLE
  79.      p1, p2 : MY_POINT ;  -- also a kinf of PRINTABLE
  80.       do
  81.          -- create the various objects
  82.      !!list1 ;
  83.      !!list2 ;
  84.      !!n1.set( 10 ) ;
  85.      !!n2.set( 20 ) ;
  86.      !!p1.set( 2, 3 ) ;
  87.      !!p2.set( 4, 5 ) ;
  88.      
  89.      list1.add_to_list( n1 ) ;
  90.      list1.add_to_list( n2 ) ;
  91.      list1.add_to_list( p1 ) ;
  92.      list2.add_to_list( n2 ) ; -- objects can be in more than one list
  93.      list2.add_to_list( p1 ) ;
  94.      list2.add_to_list( p2 ) ;
  95.      list2.add_to_list( list1 ) ; -- list 1 is an element of list 2
  96.      
  97.      put_string( "list1:%N" ) ;
  98.      list1.print_self ;
  99.      put_string( "list2:%N" ) ;
  100.      list2.print_self ;
  101.       end -- make
  102. end -- DRIVER
  103. deferred class PRINTABLE
  104.    -- insures that 'print_self' is implemented
  105. inherit
  106.    BASIC_IO
  107. feature {ANY}
  108.    print_self is
  109.      -- print yourself
  110.       deferred
  111.       end -- print_self
  112. end -- PRINTABLE
  113. class MY_NUMBER
  114.    -- holds and can print an integer
  115. inherit
  116.    PRINTABLE
  117. creation
  118.    set
  119. feature {ANY}
  120.    value : INTEGER ;
  121.    set( new_value : INTEGER ) is
  122.      -- set this number
  123.       do
  124.      value := new_value ;
  125.       end ; -- make
  126.    print_self is
  127.      -- print the value
  128.       do
  129.      put_string( "Number: " ) ;
  130.      put_int( value ) ;
  131.      put_newline ;
  132.       end ; -- print_self
  133. end -- MY_NUMBER
  134. class MY_POINT
  135.    -- holds and can print an x,y pair
  136. inherit
  137.    PRINTABLE
  138. creation
  139.    set
  140. feature {ANY}
  141.    x, y : INTEGER ;
  142.    set( new_x : INTEGER; new_y : INTEGER ) is
  143.      -- set this point
  144.       do
  145.      x := new_x ;
  146.      y := new_y
  147.       end ; -- set
  148.    print_self is
  149.      -- print the value
  150.       do
  151.      put_string( "Point: " ) ;
  152.      put_int( x ) ;
  153.      put_char( ',' ) ;
  154.      put_int( y ) ;
  155.      put_newline ;
  156.       end ; -- print_self
  157. end -- MY_POINT
  158. class ELEMENT[T]
  159.    -- holds an object reference and a
  160.    -- single link to another ELEMENT[T]
  161. creation
  162.    set_data
  163. feature {LIST}
  164.    data : T ;
  165.    next : ELEMENT[T] ;
  166.    set_data( new_data : T ) is
  167.      -- set data to the new_data
  168.       do
  169.      data := new_data
  170.       end ; -- set_data
  171.    set_next( new_next : ELEMENT[T] ) is
  172.      -- set next to the element
  173.       do
  174.      next := new_next
  175.       end ; -- set_next
  176. end -- class ELEMENT
  177. class LIST[T]
  178.    -- a generic linked list class
  179. feature {ANY}
  180.    add_to_list( data : T ) is
  181.      -- add to the end of the list
  182.       local
  183.      new_element : ELEMENT[T] ;
  184.       do
  185.      !!new_element.set_data( data ) ;
  186.      if head = void
  187.       then
  188.         head := new_element ;
  189.      else
  190.         tail.set_next( new_element ) ;
  191.      end
  192.      tail := new_element ;
  193.       end ; -- add_to_list
  194. feature {NONE}
  195.    head, tail : ELEMENT[T] ;
  196. invariant
  197.    tail_next_is_void: tail.next = void ;
  198.    tail_void_when_head_void: head = void implies tail = void
  199. end -- LIST
  200. class MY_LIST[T->PRINTABLE]
  201.    -- A printable list which holds printable data
  202. inherit
  203.    LIST[T]
  204.    PRINTABLE
  205. feature {ANY}
  206.    print_self is
  207.      -- print the list elements
  208.       local
  209.      el : ELEMENT[T] ;
  210.       do
  211.      from
  212.         el := head
  213.      until
  214.         el = void
  215.      loop
  216.         el.data.print_self
  217.         el := el.next ;
  218.      end ;
  219.       end ; -- print_self
  220. end -- MY_LIST
  221.  
  222.  
  223.  
  224.